/*
 * @Author: Troubadour 2276791354@qq.com
 * @Date: 2024-08-18 10:37:48
 * @LastEditors: Troubadour 2276791354@qq.com
 * @LastEditTime: 2024-08-18 11:16:28
 * @Version:
 * @Description:
 */

/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Include file                                                                       -
--------------------------------------------------------------------------------------------------------------------------------------*/
#include "bsp_spi.h"
#include "bsp_type.h"
#include "spi.h"

#include "cola_device.h"
#include "cola_init.h"
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal macro                                                                     -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal enum                                                                      -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal structure                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal function                                                                  -
--------------------------------------------------------------------------------------------------------------------------------------*/

static void bsp_spi_register(void);
static int bsp_spi_init(cola_device_t *dev);
static int bsp_spi_control(cola_device_t *dev, int cmd, void *args);
static int bsp_spi_write(cola_device_t *dev, int pos, const void *buffer, int size);

/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal variables                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/

static cola_device_t bsp_spi_dev;
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External variables                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Function body                                                                      -
--------------------------------------------------------------------------------------------------------------------------------------*/
static void bsp_spi_register(void)
{
    static struct cola_device_ops bsp_spi_ops = {
        .init = bsp_spi_init,
        .control = bsp_spi_control,
        .write = bsp_spi_write,
    };

    bsp_spi_dev.dops = &bsp_spi_ops;
    bsp_spi_dev.name = "bsp_spi";
    bsp_spi_dev.owner = NULL;
    cola_device_register(&bsp_spi_dev);
}
device_initcall(bsp_spi_register);

static int bsp_spi_init(cola_device_t *dev)
{
    MX_SPI1_Init();
    return 0;
}

static int bsp_spi_control(cola_device_t *dev, int cmd, void *args)
{

    switch (cmd)
    {
    case EN_SPI_CFG_MODE:
        break;

    case EN_SPI_CFG_SPEED:
        break;

    default:
        return 1;
    }
    return 0;
}

static int bsp_spi_write(cola_device_t *dev, int pos, const void *buffer, int size)
{
    return HAL_SPI_Transmit(&hspi1, (uint8_t *)buffer, size, 1000);
}


